package com.code;

import java.util.ArrayList;
import java.util.List;



public class CollectTask  {

	private String scode;
	private String state; // 0 新任务 //1 执行中 //2已完成 //3执行错误
	private int sourceStation;
	private int targetStation;
	private int sourceGroup = -1;
	private int targetGroup = -1;
	private int dtype;
	private int project;
	private int ecode;
	private String result;
	private List<CollectParam> collectParams;
    private int mcode;
    private int tcode;
	public int getMcode() {
		return mcode;
	}

	public void setMcode(int mcode) {
		this.mcode = mcode;
	}

	public int getTcode() {
		return tcode;
	}

	public void setTcode(int tcode) {
		this.tcode = tcode;
	}

	public String getScode() {
		return scode;
	}

	public void setScode(String scode) {
		this.scode = scode;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public int getSourceStation() {
		return sourceStation;
	}

	public void setSourceStation(int sourceStation) {
		this.sourceStation = sourceStation;
	}

	public int getTargetStation() {
		return targetStation;
	}

	public void setTargetStation(int targetStation) {
		this.targetStation = targetStation;
	}

	public int getSourceGroup() {
		return sourceGroup;
	}

	public void setSourceGroup(int sourceGroup) {
		this.sourceGroup = sourceGroup;
	}

	public int getTargetGroup() {
		return targetGroup;
	}

	public void setTargetGroup(int targetGroup) {
		this.targetGroup = targetGroup;
	}

	public int getDtype() {
		return dtype;
	}

	public void setDtype(int dtype) {
		this.dtype = dtype;
	}

	public int getProject() {
		return project;
	}

	public void setProject(int project) {
		this.project = project;
	}

	public int getEcode() {
		return ecode;
	}

	public void setEcode(int ecode) {
		this.ecode = ecode;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<CollectParam> getCollectParams() {
		return collectParams;
	}

	public void setCollectParams(List<CollectParam> collectParams) {
		this.collectParams = collectParams;
	}

	private static final PackageType[] packageTypes = { new PackageType("设置参数", 0x01, true),
			new PackageType("查询参数", 0x02, true), new PackageType("读新数据", 0x03, true),
			new PackageType("读数据包", 0x04, true), new PackageType("系统复位", 0x06, true),
			new PackageType("查询对象", 0x0A, true), new PackageType("通讯结束", 0x11, false),
			new PackageType("添加知识", 0x21, true), new PackageType("添加对象", 0x31, true),
			new PackageType("删除对象", 0x32, true), new PackageType("选测数据", 0x33, true),
			new PackageType("清除数据", 0x34, true), new PackageType("设置复杂参数", 0x35, true),
			new PackageType("查询复杂参数", 0x36, true), new PackageType("删除复杂参数", 0x37, true),
			new PackageType("设置目标", 0x60, true), };

	public static List<PackageType> packageTypes() {
		List<PackageType> typeList = new ArrayList<PackageType>();
		for (PackageType packageType : packageTypes) {
			typeList.add(packageType);
		}
		return typeList;
	}

	public static PackageType getPackageTypeByName(String typeName) {
		for (PackageType packageType : packageTypes) {
			if (packageType.getTypeName().equals(typeName)) {
				return packageType;
			}
		}
		return null;
	}

	public static int typeCodeOf(String typeName) {
		for (PackageType packageType : packageTypes) {
			if (packageType.getTypeName().equals(typeName)) {
				return packageType.getTypeCode();
			}
		}
		return -1;
	}

	public static PackageType getPackageTypeByCode(int typeCode) {
		for (PackageType packageType : packageTypes) {
			if (packageType.getTypeCode() == typeCode) {
				return packageType;
			}
		}
		return null;
	}

	public static boolean hasPackageTypeByCode(int typeCode) {
		for (PackageType packageType : packageTypes) {
			if (packageType.getTypeCode() == typeCode) {
				return true;
			}
		}
		return false;
	}

	public static CollectTask createCollectTask(PackageType packageType) {
		for (PackageType pt : packageTypes) {
			if (packageType.getTypeCode() == pt.getTypeCode()) {
				CollectTask collectTask = new CollectTask();
				collectTask.setDtype(packageType.getTypeCode());
				collectTask.setState("新任务");
				return collectTask;
			}
		}
		return null;
	}

	public static CollectTask createCollectTask(String typeName) {
		for (PackageType pt : packageTypes) {
			if (typeName.equals(pt.getTypeName())) {
				CollectTask collectTask = new CollectTask();
				collectTask.setDtype(pt.getTypeCode());
				collectTask.setState("新任务");
				return collectTask;
			}
		}
		return null;
	}

	public String getDname() {
		return getPackageTypeByCode(dtype).getTypeName();
	}

	public String pvalue(String pname) {
		if (collectParams == null)
			return null;
		for (CollectParam collectParam : collectParams) {
			if (collectParam.pnameIs(pname))
				return collectParam.getPvalue();
		}
		return null;
	}

	public boolean isType(String typeName) {
		return dtype == typeCodeOf(typeName);
	}

	public boolean isType(int typeCode) {
		return dtype == typeCode;
	}

	public boolean isNeedResult() {
		return getPackageTypeByCode(dtype).isNeedResult();
	}

	private String mname;
	private String tname;
	private String ename;

	public String getMname() {
		return mname;
	}

	public void setMname(String mname) {
		this.mname = mname;
	}

	public String getTname() {
		return tname;
	}

	public void setTname(String tname) {
		this.tname = tname;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	protected Integer pcode;
	protected String pname;

	public Integer getPcode() {
		return pcode;
	}

	public void setPcode(Integer pcode) {
		this.pcode = pcode;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public Long getHid() {
		if (getTargetGroup() < 2 && getEcode() < 64) {
			return getProject() * 100000000000l + getTargetStation() * 10000000 + getEcode() * 100000 + 0xFFFE;
		}
		return getProject() * 100000000000l + getTargetStation() * 10000000 + getTargetGroup() * 100000 + getEcode();
	}

	public String getPstring() {
		if (collectParams == null || collectParams.isEmpty())
			return null;
		Object[][] as = new Object[collectParams.size()][5];
		for (int i = 0; i < collectParams.size(); i++) {
			as[i] = collectParams.get(i).array();
		}
		return JsonUtil.fromArray(as);
	}

	public void setPstring(String pstring) {
		collectParams = new ArrayList<CollectParam>();
		if (!StringUtil.isEmpty(pstring)) {
			Object[] as = JsonUtil.toArray(pstring);
			for (int i = 0; i < as.length; i++) {
				Object[] a = (Object[]) as[i];
				CollectParam cp = new CollectParam();
				cp.array(a);
				collectParams.add(cp);
			}
		}
	}

	public boolean inState(String state) {
		return this.state.equals(state);
	}

}
